/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   TypeUtility.cs
 * CreateData   :   2023/7/20 16:17:21
 * UnityVersion :   2021.3.20f1c1
 * Description  :   类型转换工具
************************************************/

using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Networking;
using Random = System.Random;

namespace Main
{
    public static class TypeUtility
    {
        #region 进制转换
        #region 二进制
        public static int BinaryToDecimal(string value)//转十进制
        {
            return Convert.ToInt32(value, 2);
        }
        public static string BinaryToOctal(string value)// 转八进制
        {
            return Convert.ToString(Convert.ToInt32(value, 2), 8);
        }
        public static string BinaryToHexadecimal(string value)// 转十六进制
        {
            return Convert.ToString(Convert.ToInt32(value, 2), 16);
        }
        #endregion
        #region 八进制
        public static string OctalToBinary(string value)//转二进制
        {
            return Convert.ToString(Convert.ToInt32(value, 8), 2);
        }
        public static int OctalToDecimal(string value)//转十进制
        {
            return Convert.ToInt32(value, 8);
        }
        public static string OctalToHexadecimal(string value)//转十六进制
        {
            return Convert.ToString(Convert.ToInt32(value, 8), 16);
        }
        #endregion
        #region 十进制
        public static string DecimalToBinary(int value)// 转二进制
        {
            return Convert.ToString(value, 2);
        }
        public static string DecimalToOctal(int value)    //  转八进制
        {
            return Convert.ToString(value, 8);
        }
        public static string DecimalToHexadecimal(int value)  // 转十六进制
        {
            return Convert.ToString(value, 16);
        }
        #endregion
        #region 十六进制
        public static string HexadecimalToBinary(string value)  // 转二进制
        {
            return Convert.ToString(Convert.ToInt32(value, 16), 2);
        }
        public static string HexadecimalToOctal(string value)  // 转八进制
        {
            return Convert.ToString(Convert.ToInt32(value, 16), 8);
        }
        public static int HexadecimalToDecimal(string value)  // 转十进制
        {
            return Convert.ToInt32(value, 16);
        }
        #endregion
        #endregion
        #region 随机数
        /// <summary>
        /// 返回[min,max]区间随机数
        /// </summary>
        /// <param name="min">包含最小值</param>
        /// <param name="max">包含最大值</param>
        /// <returns></returns>
        public static int RandomInt(int min, int max)
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());   // 用来计算伪随机数序列起始值的数字
            return random.Next(min, max + 1);
        }
        /// <summary>
        /// 返回一个大于或等于 0.0 且小于 1.0 的随机浮点数。
        /// </summary>
        /// <returns></returns>
        public static double RandomDouble()
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());   // 用来计算伪随机数序列起始值的数字
            return random.NextDouble();
        }
        #endregion
        #region 保留小数
        /// <summary>
        /// 返回保留几位小数的浮点数（不四舍五入）
        /// </summary>
        /// <param name="value">原始浮点数</param>
        /// <param name="count">小数点数量</param>
        /// <returns></returns>
        public static float NumberOfDecimalsRegex(float value, int count)
        {
            Match match = Regex.Match(value.ToString(), "^[0-9]*\\.[0-9]{" + count + "}");
            if (match.Success)
            {
                return float.Parse(match.Value);
            }
            return value;
        }
        /// <summary>
        /// 返回保留几位小数的浮点数（四舍五入）
        /// </summary>
        /// <param name="value">原始浮点数</param>
        /// <param name="count">保留几位小数</param>
        /// <returns></returns>
        public static float NumberOfDecimalsRound(float value, int count)
        {
            if (count > 0 && count < 16)
            {
                value = (float)Math.Round(value, count);
                //value = (float)decimal.Round((decimal)value, 2);
                //value = float.Parse(value.ToString("f2"));
                //value = float.Parse(value.ToString("#0.00"));
                //value = float.Parse(string.Format("{0:N2}", value));
            }

            return value;
        }
        #endregion
        #region 字节与字符串
        /// <summary>
        /// 字符串转字节数组(Unicode 编码)
        /// </summary>
        /// <returns></returns>
        public static byte[] StringToBinaryUnicode(String value)
        {
            return Encoding.Unicode.GetBytes(value);
        }
        /// <summary>
        /// 字节数组转字符串(Unicode 编码)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string BinaryToStringUnicode(byte[] value)
        {
            return Encoding.Unicode.GetString(value);
        }
        /// <summary>
        /// 字符串转字节数组(ASCII 编码 不支持中文)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] StringToBinaryASCII(string value)
        {
            return Encoding.ASCII.GetBytes(value);
        }
        /// <summary>
        /// 字节数组转字符串(ASCII 编码 不支持中文)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string BinaryToStringASCII(byte[] value)
        {
            return Encoding.ASCII.GetString(value);
        }
        #endregion
        #region 编码
        /// <summary>
        /// 字符串转化Unicode 编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string StringToUnicode(String value)
        {
            var bytes = Encoding.Unicode.GetBytes(value);
            var stringBuilder = new StringBuilder();
            for (var i = 0; i < bytes.Length; i += 2)
            {
                stringBuilder.AppendFormat("\\u{0:x2}{1:x2}", bytes[i + 1], bytes[i]);
            }
            return stringBuilder.ToString();
        }
        /// <summary>
        /// Unicode 编码转化 字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string UnicodeToString(String value)
        {
            return Regex.Unescape(value);
        }
        /// <summary>
        /// 字符转ASCII码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int CharToASCII(char value)
        {
            return value;
        }
        /// <summary>
        /// ASCII码转字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static char ASCIIToChar(int value)
        {
            return (char)value;
        }
        /// <summary>
        /// url字符串编码
        /// </summary>
        public static string UrlEncode(string url, Encoding encoding)
        {
            return UnityWebRequest.EscapeURL(url, encoding);
        }
        /// <summary>
        /// url字符串解码
        /// </summary>
        public static string UrlDecode(string url,Encoding encoding)
        {
            return UnityWebRequest.UnEscapeURL(url, encoding);
        }
        #endregion
        #region 序列化和反序列化
        /// <summary>
        /// 对象转 字节，
        /// 要序列对象 需要添加“[Serializable]”.
        /// 不需要序列化的字段添加 “[NonSerialized]”
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] ObjToBinary(object value)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream())
            {
                formatter.Serialize(ms, value);
                return ms.ToArray();
            }
        }
        /// <summary>
        /// 字节数组转对象
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object BinaryToObj(byte[] value)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream(value))
            {
                return formatter.Deserialize(ms);
            }
        }
        #endregion
        #region 反射
        #endregion
        #region 集合
        #endregion
    }
}
